package com.westmarine.qr_hybris.util;

import java.awt.AWTException;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import javax.imageio.ImageIO;

import org.monte.media.Format;
import org.monte.media.FormatKeys.MediaType;
import org.monte.media.math.Rational;
import org.monte.screenrecorder.ScreenRecorder;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;

import static org.monte.media.FormatKeys.EncodingKey;
import static org.monte.media.FormatKeys.FrameRateKey;
import static org.monte.media.FormatKeys.KeyFrameIntervalKey;
import static org.monte.media.FormatKeys.MIME_AVI;
import static org.monte.media.FormatKeys.MediaTypeKey;
import static org.monte.media.FormatKeys.MimeTypeKey;
import static org.monte.media.VideoFormatKeys.CompressorNameKey;
import static org.monte.media.VideoFormatKeys.DepthKey;
import static org.monte.media.VideoFormatKeys.ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE;
import static org.monte.media.VideoFormatKeys.QualityKey;
import static org.testng.AssertJUnit.assertEquals;

public class MethodUtil {
	
	public static String uniqueEmailStr() {
		java.util.Date date= new java.util.Date();
		String email="nd9594+"+date.getTime()+"@gmail.com";
		return email;
	}
	
	public static File captureElementBitmap(WebElement element) throws Exception {   

	   	File screen = ((TakesScreenshot)  Browser.driver()).getScreenshotAs(OutputType.FILE);	   	
	   	BufferedImage img = ImageIO.read(screen);	   	
	   	// Get the Width and Height of the WebElement 
	   	int width = element.getSize().getWidth();
	    int height = element.getSize().getHeight();
	    //Create a rectangle
	    Rectangle rect = new Rectangle(width, height);	    
	    //Get the Location of WebElement in a Point. 
	    Point p = element.getLocation();	    
	    BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width, rect.height);	    
	    //Write image data of element in File object
	    ImageIO.write(dest, "png", screen);
	    return screen;
	 }
	
	public static ScreenRecorder getScreenRecorder() throws IOException, AWTException{
		GraphicsConfiguration gc = GraphicsEnvironment
				.getLocalGraphicsEnvironment()
				.getDefaultScreenDevice()
				.getDefaultConfiguration();
		return new ScreenRecorder(gc,new Format(MediaTypeKey, MediaType.FILE, MimeTypeKey, MIME_AVI), new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey,ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,DepthKey, (int)24, FrameRateKey, Rational.valueOf(15),QualityKey, 1.0f,KeyFrameIntervalKey, (int) (15 * 60)),new Format(MediaTypeKey, MediaType.VIDEO,EncodingKey,"black",FrameRateKey, Rational.valueOf(30)),null);
	}
}
